package com.blue.Impl.ai;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blue.common.constant.CommonConstant;
import com.blue.common.entity.LoginUserInfo;
import com.blue.common.exception.CustomException;
import com.blue.common.utils.ThreadLocalUtils;
import com.blue.dto.ai.ChatRecordDTO;
import com.blue.mapper.ai.UserSubscribeMapper;
import com.blue.po.ai.UserSubscribePO;
import com.blue.service.ai.IUserSubscribeService;
import com.blue.vo.ai.ChatRecordVO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Service
public class UserSubscribeServiceImpl extends ServiceImpl<UserSubscribeMapper, UserSubscribePO> implements IUserSubscribeService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void checkSubscribe(ChatRecordDTO requestDTO) {
        //校验订阅
        LoginUserInfo loginUserInfo = ThreadLocalUtils.getLoginUserInfo();
        String userId = loginUserInfo.getUserId();
        List<UserSubscribePO> list = this.list(new LambdaQueryWrapper<UserSubscribePO>()
                .select(UserSubscribePO::getId)
                .eq(UserSubscribePO::getUserId, userId)
                .eq(UserSubscribePO::getIsFlag, CommonConstant.YES));
        if(CollectionUtils.isEmpty(list)){
            throw new CustomException("你还未订阅blue呢，订阅之后再和blue聊天吧~");
        }
        requestDTO.setUserId(userId);
        //记录信息
        rabbitTemplate.convertAndSend("chatExchange","chatKey", JSON.toJSONString(requestDTO));
    }
}
